﻿using System;
using System.Collections.Generic;
using System.Threading;
using CustomTask;
using System.Reflection; 

namespace OpenSesame
{ 
    public class OpenSesame_v3
    {
        static void Main(string[] args)
        {   
            Type type ;
            ProgramStatementBlock openSesameProgram = new ProgramStatementBlock();

            //F:\Project\JingweiWorkflow\Trunk\JingWeiWorkFlow\CustomTask\bin\Debug\CustomTask.dll
            //Assembly assembly = Assembly.LoadFile(@"C:\SVNWorkingCopy\FrontendV3\Working\Trunk\JingWeiWorkFlow\CustomTask\bin\Debug\CustomTask.dll");

            //type = assembly.GetType("CustomTask.PrintKey");
            //ProgramStatement printKey = Activator.CreateInstance(type) as ProgramStatement;

            //type = assembly.GetType("CustomTask.Read");
            //ProgramStatement read = Activator.CreateInstance(type) as ProgramStatement;

            //type = assembly.GetType("CustomTask.PrintGreeting");
            //ProgramStatement printGreeting = Activator.CreateInstance(type) as ProgramStatement;

            PrintKey printKey = new PrintKey();
            printKey.Name = "p1";
            Read read = new Read();
            read.Name = "r1";

            ActivityBind bindPrintKey_Key = new ActivityBind();
            bindPrintKey_Key.Name = "p1";
            bindPrintKey_Key.Path = "Key";

            ActivityBind bindRead_Text = new ActivityBind();
            bindRead_Text.Name = "r1";
            bindRead_Text.Path = "Text";
            //printKey.SetBinding(PrintKey.KeyProperty, bind);

            PrintGreeting printGreeting = new PrintGreeting();
            printGreeting.Name = "p1";
            printGreeting.SetBinding(PrintGreeting.KeyProperty, bindPrintKey_Key);
            printGreeting.SetBinding(PrintGreeting.InputProperty, bindRead_Text);

            //Using Dependency control to pass the value to PrintGreeting.Key
            //printGreeting.Key = printKey.Key; // bind to printKey.Key
            //printGreeting.Input = read.Input; // bind to read.Text
            openSesameProgram.Statements.Add(printKey);
            openSesameProgram.Statements.Add(read);
            openSesameProgram.Statements.Add(printGreeting);
            
            MythicalRuntime runtime = new MythicalRuntime();
            ProgramHandle handle = runtime.RunProgram(openSesameProgram);

            string keyValueReadIn = Console.ReadLine();
            //Bookmark的下半部分，用新的thread执行
            ThreadPool.QueueUserWorkItem(state => runtime.Mgr.Resume("read", state), keyValueReadIn);

            // keep the main thread running
            Console.ReadLine();
        }
    }
}

//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading;
//using System.Runtime.Remoting.Messaging;
//namespace OpenSesame
//{ 
//     delegate string AsyncReadLine();
//    class Program
//    {
//        static string key;
//        static void Main(string[] args)
//        {
//            BookmarkManager mgr = new BookmarkManager();
//            OpenSesame openSesameProgram = new OpenSesame();
//            openSesameProgram.start(mgr);

//            mgr.Resume("read", null);
//        }
         
//    }

//    public class Bookmark
//    {
//        public Bookmark(string name, BookmarkLocation continueAt)
//        {

//            Name = name;
//            ContinueAt = continueAt;
            
             
//        }


//        public string Name { get; set; }
//        public BookmarkLocation ContinueAt { get; set; }
//        public object PayLoad { get; set; }

//        public BookmarkManager BookmarkManager { get; set; }
//    }

//    public delegate void BookmarkLocation (Bookmark resumed);

//    public class BookmarkManager
//    {
//        private List<Bookmark> bookmarkList;
//        public BookmarkManager()
//        {
//            bookmarkList = new List<Bookmark>();
//        }
//        public void Add(Bookmark bookmark)
//        {
//            bookmarkList.Add(bookmark);
//            bookmark.BookmarkManager = this;
//        }
//        public void Remove(Bookmark bookmark)
//        {
//            bookmarkList.Remove(bookmark);
//        }
//        public void Resume(string bookmarkName, object payload)
//        {
//            foreach (Bookmark bookmark in bookmarkList)
//            {
//                if (bookmark.Name == bookmarkName)
//                {
//                    bookmark.PayLoad = payload;
//                    bookmark.ContinueAt(bookmark);
//                    break;
//                }
//            }
//        }
//    }

//    [Serializable]
//    public class OpenSesame
//    {
//        static string key;
//        public void start(BookmarkManager mgr)
//        {
//            key = DateTime.Now.Millisecond.ToString();
//            Console.WriteLine("Here is your key: " + key);
//            mgr.Add(new Bookmark("read", ContinueAt));

//        }

//        static void ContinueAt(Bookmark resumed)
//        {
//            string s = (string)resumed.PayLoad;
//            BookmarkManager mgr = resumed.BookmarkManager;
//            mgr.Remove(resumed);
            
//            if (key.Equals(s))
//            {
//                Console.WriteLine("hello, world");
//                Environment.Exit(0);
//            }
//        }
//    }
//}
